import math
import json
from types import FunctionType
import os

import game
import dialog
import localizing
import resources
import save
import ez_utils
from ui import print, input, endPassage

class StartMenu:
    """
    开始菜单的选项所注册的函数。
    """
    @staticmethod
    def choicesStartMenu(cm:dialog.ChoiceManager) -> None:
        cm  .addChoice(dialog.Choice("textdungeon.mainmenu.start",StartMenu.choiceStartGame)) \
            .addChoice(dialog.Choice("textdungeon.mainmenu.monuments", StartMenu.choiceMonuments)) \
            .addChoice(dialog.Choice("textdungeon.mainmenu.settings",StartMenu.choiceSettings)) \
            .addChoice(dialog.Choice("textdungeon.mainmenu.exit",StartMenu.choiceExit)) \
            .Run()
    
    @staticmethod
    def choiceStartGame() -> None:
        game.Game.I.status["where"] = "game"

    @staticmethod
    def choiceMonuments() -> None:
        game.Game.I.status["where"] = "monuments"
    
    @staticmethod
    def choiceSettings() -> None:
        game.Game.I.status["where"] = "settings"

    @staticmethod
    def choiceExit() -> None:
        print( localizing.Localizer.I.get("textdungeon.mainmenu.bye") )
        game.Game.I.status["where"] = "exit"

class Settings:
    """
    设置页面。
    """
    @staticmethod
    def choicesSettingsRoot(cm:dialog.ChoiceManager) -> None:
        while True:
            cm  .addChoice(dialog.Choice("textdungeon.settings.language",SettingsLanguage.choicesLanaguage(cm))) \
                .addChoice(dialog.Choice("textdungeon.settings.exit",Settings.choiceExitRoot)) \
                .Run()
            if game.Game.I.status["where"] == "startmenu": break
    
    @staticmethod
    def choiceExitRoot() -> None:
        game.Game.I.status["where"] = "startmenu"

class SettingsLanguage:
    @staticmethod
    def choicesLanaguage(cm:dialog.ChoiceManager) -> FunctionType:
        def _func():
            game.Game.I.status["where"] == "settings.language"
            endPassage()

            cm  .addChoice(dialog.Choice("textdungeon.settings.language.zh_cn", SettingsLanguage.choiceZH_CN)) \
                .addChoice(dialog.Choice("textdungeon.settings.language.en_us", SettingsLanguage.choiceEN_US)) \
                .Run()
            
            game.Game.I.status["where"] =="settings"
        return _func
    def choiceZH_CN():
        localizing.Localizer.I.loadLanguage("zh_cn")
        game.Game.I.save.doSaveGlobalFuncs()
    def choiceEN_US():
        localizing.Localizer.I.loadLanguage("en_us")
        game.Game.I.save.doSaveGlobalFuncs()

class Monuments:
    """
    浏览查看纪念碑。
    """
    @staticmethod
    def showMonument(data:dict) -> None:

        time = localizing.Localizer.I.get("textdungeon.time")

        game.Game.I.status["where"] = "startmenu"
        print(localizing.Localizer.I.get("textdungeon.mainmenu.monuments"),"-",data["name"])

        print(time.format(**ez_utils.getTimeString(data["startTime"])),"-",time.format(**ez_utils.getTimeString(data["endTime"])))

        print("----", localizing.Localizer.I.get( data["description"] ))
        endPassage()


    @staticmethod
    def getChoiceMonument(r:resources.Resource) -> None:
        def _f():
            # 读取
            r.read()
            data = json.load(r.file)
            r.close()
            # 展示
            Monuments.showMonument(data)
        return _f
    
    @staticmethod
    def previousPage() -> None:
        game.Game.I.status["monuments_page"]-=1

    @staticmethod
    def nextPage() -> None:
        game.Game.I.status["monuments_page"]+=1

    def exit() -> None:
        game.Game.I.status["where"] = "startmenu"

    @staticmethod
    def choicesMonuments(cm:dialog.ChoiceManager) -> None:
        """
        实现翻页效果。
        """
        os.makedirs(game.Game.I.save.monument_path,exist_ok=True)
        monuments = list( resources.Resource.create(save.Save.I.monument_path).children.values() )
        if len(monuments) <= 0: 
            print(localizing.Localizer.I.get("textdungeon.mainmenu.monuments.nothinghere"))
            endPassage()
            return
        max_page = game.Game.I.status["monuments_max_page"] = math.ceil( len(monuments) / 8 )
        game.Game.I.status["monuments_page"] = 0

        while True:
            if game.Game.I.status["monuments_page"] > 0: cm.addChoice(dialog.Choice("textdungeon.mainmenu.monuments.previouspage",Monuments.previousPage))
            page_count = 8 if game.Game.I.status["monuments_page"] < (max_page - 1) or len(monuments) % 8 == 0 else len(monuments) % 8

            # 输出选项
            for i in range(page_count):
                m = monuments[game.Game.I.status["monuments_page"]*8+i]
                cm.addChoice(dialog.Choice("textdungeon.mainmenu.monuments.monument",Monuments.getChoiceMonument(m),m.name))

            if game.Game.I.status["monuments_page"] < max_page-1: cm.addChoice(dialog.Choice("textdungeon.mainmenu.monuments.nextpage",Monuments.nextPage))

            cm.addChoice(dialog.Choice("textdungeon.mainmenu.monuments.exit",Monuments.exit))

            cm.Run()

            if game.Game.I.status["where"] == "startmenu": break
    
